﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using DevExpress.XtraEditors.Repository;


namespace Marmat.Forms
{
    [ToolboxItem(true)]
    [DefaultEvent("ValueChanged")]
    [Designer("Marmat.Forms.Design.TextBoxDesigner")]
    [ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
    public partial class TextBox : EditControl, IRequiredComponentSupport
    {
        public TextBox()
        {

            // 此调用是设计器所必需的。
            this.InitializeComponent();

            // 在 InitializeComponent() 调用之后添加任何初始化。
            this.BaseEdit = this.TextEdit1;
            this.BaseEdit.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
            this.TextEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            this.TextEdit1.KeyDown += this.m_BaseEdit_KeyDown;
        }

        public new Size Size
        {
            get => base.Size;
            set
            {
                base.Size = value;
                this.CalcHeight();
            }
        }

        /// <summary>
        /// 获取或设置控件的文本编辑风格。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(typeof(TextEditStyles), "Standard")]
        [Category("外观")]
        [Description("获取或设置控件的文本编辑风格。")]
        public virtual TextEditStyles TextEditStyle
        {
            get => this.m_TextEditStyle;
            set
            {
                this.m_TextEditStyle = value;
                this.TextEdit1.Properties.TextEditStyle = (DevExpress.XtraEditors.Controls.TextEditStyles)value;
            }
        }

        private TextEditStyles m_TextEditStyle = TextEditStyles.Standard;

        /// <summary>
        /// 获取或设置控件的文本值
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(null)]
        public new string Value
        {
            get
            {
                if (base.Value is null || ReferenceEquals(base.Value, DBNull.Value))
                {
                    return null;
                }
                return Convert.ToString(base.Value);
            }
            set => base.Value = value;
        }

        /// <summary>
        /// 获取或设置文本框的水印文本。
        /// </summary>
        /// <returns></returns>
        [DefaultValue("")]
        [Category("行为")]
        [Description("获取或设置文本框的水印文本。")]
        public virtual string WatermarkText
        {
            get => this.TextEdit1.Properties.NullValuePrompt;
            set => this.TextEdit1.Properties.NullValuePrompt = value;
        }

        /// <summary>
        /// 获取或设置字符，该字符用于屏蔽单行 System.Windows.Forms.TextBox 控件中的密码字符。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue('\0')]
        [Category("行为")]
        [Description("获取或设置字符，该字符用于屏蔽单行 System.Windows.Forms.TextBox 控件中的密码字符。")]
        public char PasswordChar
        {
            get
            {
                if (this.BaseEdit.Properties is RepositoryItemTextEdit textEdit)
                    return textEdit.PasswordChar;
                return default;
            }
            set
            {
                if (this.BaseEdit.Properties is RepositoryItemTextEdit textEdit)
                    textEdit.PasswordChar = value;
            }
        }

        /// <summary>
        /// 获取或设置控件是否允许编辑。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(true)]
        [Category("行为")]
        [Description("获取或设置控件是否允许编辑。")]
        public bool AllowEdit
        {
            get => this.m_AllowEdit;
            set => this.m_AllowEdit = value;
        }

        private bool m_AllowEdit = true;

        /// <summary>
        /// 获取或设置一个值，该值指示 System.Windows.Forms.TextBox 控件中的文本是否应该以默认的密码字符显示。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(false)]
        [Category("行为")]
        [Description("获取或设置一个值，该值指示 System.Windows.Forms.TextBox 控件中的文本是否应该以默认的密码字符显示。")]
        public bool UseSystemPasswordChar
        {
            get => this.TextEdit1.Properties.UseSystemPasswordChar;
            set => this.TextEdit1.Properties.UseSystemPasswordChar = value;
        }

        private void m_BaseEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                SendKeys.Send("{tab}");
        }

        public virtual bool Verify()
        {
            return this.BaseEdit.EditValue is not null && !ReferenceEquals(this.BaseEdit.EditValue, DBNull.Value) && !string.IsNullOrEmpty(this.BaseEdit.EditValue.ToString());
        }

        public virtual void OpenStyle()
        {
            this.BaseEdit.BackColor = Color.FromArgb(199, 237, 204);
        }

        public virtual void CloseStyle()
        {
            this.BackColor = this.BackColor;
        }
    }
}